package com.lanting.meetingserver.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import com.lanting.constant.Constants;

public class SynSysUtil {
	
	public static Boolean verifyifexist(int meetingId,int pptId) {
		try {
			URL actionURL  = new URL(Constants.SYN_SYS_BASE_URL + "/rest/sensor/verifyifexist/" + meetingId + "/" + pptId);
			BufferedReader br = new BufferedReader(new InputStreamReader(actionURL.openStream()));
			StringBuilder sb = new StringBuilder();
			String strTemp = "";
			while (null != (strTemp = br.readLine())) {
				sb.append(strTemp);
			}
			return sb.toString().contains("success");
		} catch (MalformedURLException e) {
			throw new IllegalStateException("无效的地址！",e);
		} catch (IOException e) {
			throw new IllegalStateException("同步系统当前不可用！",e);
		}
	}
	
	public static Boolean append(int meetingId,int pptId) {
		try {
			URL actionURL  = new URL(Constants.SYN_SYS_BASE_URL + "/rest/sensor/append/" + meetingId + "/" + pptId);
			BufferedReader br = new BufferedReader(new InputStreamReader(actionURL.openStream()));
			StringBuilder sb = new StringBuilder();
			String strTemp = "";
			while (null != (strTemp = br.readLine())) {
				sb.append(strTemp);
			}
			return sb.toString().contains("success");
		} catch (MalformedURLException e) {
			throw new IllegalStateException("无效的地址！",e);
		} catch (IOException e) {
			throw new IllegalStateException("同步系统当前不可用！",e);
		}
	}
	
	public static Boolean releaseLock(int meetingId,int pptId) {
		try {
			URL actionURL  = new URL(Constants.SYN_SYS_BASE_URL + "/rest/control/" + meetingId+"/" + pptId + "/0/0");
			actionURL.openStream();
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	public static Boolean remove(int meetingId,int pptId) {
		try {
			URL actionURL  = new URL(Constants.SYN_SYS_BASE_URL + "/rest/sensor/remove/" + meetingId+"/" + pptId);
			BufferedReader br = new BufferedReader(new InputStreamReader(actionURL.openStream()));
			StringBuilder sb = new StringBuilder();
			String strTemp = "";
			while (null != (strTemp = br.readLine())) {
				sb.append(strTemp);
			}
			return sb.toString().contains("success");
		} catch (MalformedURLException e) {
			throw new IllegalStateException("无效的地址！",e);
		} catch (IOException e) {
			throw new IllegalStateException("同步系统当前不可用！",e);
		}
	}
}
